/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.service.probe;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.cache.AttributeCache;
import com.sinodata.bsm.center.cache.CollectFieldCache;
import com.sinodata.bsm.center.cache.CollectParamCache;
import com.sinodata.bsm.center.cache.CollectScheduleCache;
import com.sinodata.bsm.center.cache.CollectTaskCache;
import com.sinodata.bsm.center.cache.CollectorCache;
import com.sinodata.bsm.center.cache.DataTypeCache;
import com.sinodata.bsm.center.cache.EventTypeCache;
import com.sinodata.bsm.center.cache.NetWorkCache;
import com.sinodata.bsm.center.cache.PropertyCache;
import com.sinodata.bsm.center.cache.ProtocolParameterCache;
import com.sinodata.bsm.center.cache.ResAttrValCache;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.cache.ResFindDefCache;
import com.sinodata.bsm.center.cache.ResRelationCache;
import com.sinodata.bsm.center.cache.ResTypeCache;
import com.sinodata.bsm.common.vo.Attribute;
import com.sinodata.bsm.common.vo.CollectField;
import com.sinodata.bsm.common.vo.CollectParam;
import com.sinodata.bsm.common.vo.CollectSchedule;
import com.sinodata.bsm.common.vo.CollectTask;
import com.sinodata.bsm.common.vo.Collector;
import com.sinodata.bsm.common.vo.DataType;
import com.sinodata.bsm.common.vo.EventType;
import com.sinodata.bsm.common.vo.NwDeviceDef;
import com.sinodata.bsm.common.vo.NwDeviceIndicator;
import com.sinodata.bsm.common.vo.NwDeviceSysoid;
import com.sinodata.bsm.common.vo.NwIndicatorDef;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.ProtocolParameter;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResAttrVal;
import com.sinodata.bsm.common.vo.ResFindDef;
import com.sinodata.bsm.common.vo.ResRelation;
import com.sinodata.bsm.common.vo.ResType;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-7 下午5:42:47          tangli_ITSM        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional
public class ProbeLoadService {
    private static final Logger logger = Logger.getLogger(ProbeLoadService.class);

    @Autowired
    private AttributeCache attributeCache;
    @Autowired
    private CollectTaskCache collectTaskCache;
    @Autowired
    private PropertyCache propertyCache;
    @Autowired
    private ResAttrValCache resAttrValCache;
    @Autowired
    private CollectFieldCache collectFieldCache;
    @Autowired
    private CollectScheduleCache collectScheduleCache;
    @Autowired
    private CollectParamCache collectParamCache;
    @Autowired
    private ResTypeCache resTypeCache;
    @Autowired
    private CollectorCache collectorCache;
    @Autowired
    private ResFindDefCache resFindDefCache;
    @Autowired
    private ResRelationCache resRelationCache;
    @Autowired
    private DataTypeCache dataTypeCache;
    @Autowired
    private ProtocolParameterCache protocolParameterCache;
    @Autowired
    private ResCache resCache;
    @Autowired
    private NetWorkCache netWorkCache;

    @Autowired
    private EventTypeCache eventTypeCache;

    public ProbeLoadService() {
    }

    public Map<Long, NwDeviceDef> getDevMap(Long probeId) {
        return netWorkCache.getDeviceMap();
    }

    public Map<Long, NwIndicatorDef> getIndicMap(Long probeId) {
        return netWorkCache.getIndicMap();
    }

    public Map<String, NwDeviceSysoid> getDevSysoidMap(Long probeId) {
        return netWorkCache.getDevSysoidMap();
    }

    public Map<Long, List<NwDeviceIndicator>> getDevIndicMap(Long probeId) {
        return netWorkCache.getDevIndicMap();
    }

    /**
     * 获取Probe上资源属性
     * @param probeId
     * @return
     */

    public List<Attribute> getAttributes(Long probeId) {
        List<Attribute> list = attributeCache.getAll();
        List<Attribute> response = new ArrayList<Attribute>();
        List<Res> probeRes = resCache.getResByProbe(probeId);
        for (Attribute attr : list) {
            if (attr.getResId() == null) {
                response.add(attr);
            } else {
                Long resId = attr.getResId();
                Res res = resCache.get(resId);
                if (res != null && probeRes.contains(res)) {
                    response.add(attr);
                }
            }
        }
        return response;
    }

    /**
     * 获取Probe上资源采集器实例设置
     * @param probeId
     * @return
     */

    public List<CollectTask> getCollectTasks(Long probeId) {
        List<CollectTask> list = collectTaskCache.getAll();
        List<CollectTask> response = new ArrayList<CollectTask>();
        List<Res> probeRes = resCache.getResByProbe(probeId);
        for (CollectTask c : list) {
            Long resId = c.getResId();
            if (resId == null) {
                response.add(c);
            } else {
                Res res = resCache.get(resId);
                if (res != null && probeRes.contains(res)) {
                    response.add(c);
                }
            }
        }
        return response;
    }

    /**
     * 获取Probe上指标
     * @param probeId
     * @return
     */

    public List<Property> getProperties(Long probeId) {
        List<Property> list = propertyCache.getAll();
        List<Property> response = new ArrayList<Property>();
        List<Res> probeRes = resCache.getResByProbe(probeId);
        for (Property p : list) {
            Long resId = p.getResId();
            if (resId == null) {
                response.add(p);
            } else {
                Res res = resCache.get(resId);
                if (res != null && probeRes.contains(res)) {
                    response.add(p);
                }
            }
        }
        return response;
    }

    /**
     * 获取Probe上资源属性值
     * @param probeId
     * @return
     */

    public List<ResAttrVal> getResAttrVals(Long probeId) {
        List<ResAttrVal> list = resAttrValCache.getAll();
        List<ResAttrVal> response = new ArrayList<ResAttrVal>();
        List<Res> probeRes = resCache.getResByProbe(probeId);
        for (ResAttrVal r : list) {
            Long resId = r.getResId();
            if (resId == null) {
                response.add(r);
            } else {
                Res res = resCache.get(resId);
                if (res != null && probeRes.contains(res)) {
                    response.add(r);
                }
            }
        }
        return response;
    }

    public List<EventType> getEventTypes(Long probeId) {
        return eventTypeCache.getAll();
    }

    public List<Res> getReses(Long probeId) {
        return resCache.getResByProbe(probeId);
    }

    public List<ResType> getResTypes() {
        return resTypeCache.getAll();
    }

    /**
     * 获取Probe上采集任务的参数配置
     * 根据采集任务参数关联的采集任务是否为Probe的管理范围内
     * @param probeId
     * @return
     */
    public List<CollectField> getCollectFields(Long probeId) {
        List<CollectField> list = collectFieldCache.getAll();
        List<CollectField> response = new ArrayList<CollectField>();
        List<CollectTask> collTasks = getCollectTasks(probeId);
        Set<Long> collTaskIdSet = new HashSet<Long>();
        for (CollectTask e : collTasks) {
            collTaskIdSet.add(e.getId());
        }
        for (CollectField e : list) {
            Long collectTaskId = e.getCollectTaskId();
            if (collTaskIdSet.contains(collectTaskId)) {
                response.add(e);
            }
        }
        return response;
    }

    /**
     * 获取probe上采集采集调度
     * @param probeId
     * @return
     */
    public List<CollectSchedule> getCollectSchedules(Long probeId) {
        List<CollectSchedule> list = collectScheduleCache.getAll();
        List<CollectSchedule> response = new ArrayList<CollectSchedule>();
        List<CollectTask> collTasks = getCollectTasks(probeId);
        Set<Long> collTaskIdSet = new HashSet<Long>();
        for (CollectTask e : collTasks) {
            collTaskIdSet.add(e.getId());
        }
        for (CollectSchedule e : list) {
            Long collectTaskId = e.getCollectTaskId();
            if (collTaskIdSet.contains(collectTaskId)) {
                response.add(e);
            }
        }
        return response;
    }

    /**
     * 获取probe上采集参数配置
     * @param probeId
     * @return
     */

    public List<CollectParam> getCollectParams(Long probeId) {
        List<CollectParam> list = collectParamCache.getAll();
        List<CollectParam> response = new ArrayList<CollectParam>();
        List<CollectTask> collTasks = getCollectTasks(probeId);
        Set<Long> collTaskIdSet = new HashSet<Long>();
        for (CollectTask e : collTasks) {
            collTaskIdSet.add(e.getId());
        }
        for (CollectParam e : list) {
            Long collectTaskId = e.getCollectTaskId();
            if (collTaskIdSet.contains(collectTaskId)) {
                response.add(e);
            }
        }
        return response;
    }

    public List<Collector> getCollectors() {
        return collectorCache.getAll();
    }

    public List<DataType> getDataTypes() {
        return dataTypeCache.getAll();
    }

    public List<ProtocolParameter> getProtocolParameters() {
        return protocolParameterCache.getAll();
    }

    public List<ResFindDef> getResFindDefs() {
        return resFindDefCache.getAll();
    }

    /**
     * 获取probe上资源关系
     * @param probeId
     * @return
     */
    public List<ResRelation> getResRelations(Long probeId) {
        Map<Long, List<ResRelation>> map = resRelationCache.getAll();
        List<ResRelation> response = new ArrayList<ResRelation>();
        List<Res> probeRes = resCache.getResByProbe(probeId);
        Set<Long> set = new HashSet<Long>();
        for (Res e : probeRes) {
            set.add(e.getId());
            List<ResRelation> list = map.get(e.getId());//每个资源几个关系
            if (list != null) {
                for (ResRelation r : list) {
                    if (set.contains(r.getStartId()) && set.contains(r.getEndId())) {
                        response.add(r);
                    }
                }
            }
        }
        return response;
    }

}